//
//  ApiClient.m
//  PywSdk
//
//  Created by liangyidong on 15/12/24.
//  Copyright © 2015年 zero. All rights reserved.
//

#import "ApiClient.h"
#import "ApiClientConst.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>



@implementation ApiClient

signleton_m(ApiClient)

@synthesize isDebug;

+ (void) setDebug: (BOOL)isDebug {
    isDebug = isDebug;
}

- (BOOL)isReachable
{
    if ([YYFTAFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) {
        return YES;
    } else {
        return [[YYFTAFNetworkReachabilityManager sharedManager] isReachable];
    }
}
             
/**
 *  开始网络|监听网络状态
 */
- (void)reachabilityStartMonitoringAndStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block
{
    [[YYFTAFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[YYFTAFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (block) {
            block(status);
        }
    }];
}




/**
 *  网络类型
 *
 *  @return 例：中国移动|WIFI
 */
- (NSString *)networkInfo
{
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [info subscriberCellularProvider];
    NSString *carrierName = @"Unknown";
    if (carrier.carrierName.length > 0) {
        carrierName = carrier.carrierName;
    }
    
    NSString *networkInfo = [NSString stringWithFormat:@"%@|%@",carrierName,[self networkType]];;
    return networkInfo;
}


- (NSString *)networkType {
    __block NSString *networkStatus = @"NotReachable";
    AFNetworkReachabilityStatus status = [YYFTAFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
        {
            networkStatus = @"Unknown";
        }
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
        {
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            NSString *currentRadioAccessTechnology = [info currentRadioAccessTechnology];
            if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
                networkStatus = @"4G";
            }else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
                networkStatus = @"2G";
            }else {
                networkStatus = @"3G";
            }
        }
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
        {
            networkStatus = @"WiFi";
        }
            break;
            
        default:
            break;
    }
    return networkStatus;
}


@end
